package cc.yugu.yhdw.webserver.service.basic;


import cc.yugu.yhdw.common.constant.CacheKey;
import cc.yugu.yhdw.common.domain.basic.DictItem;
import cc.yugu.yhdw.webserver.persistence.basic.DictItemMapper;
import com.whalin.MemCached.MemCachedClient;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 字典业务切面类
 *
 * @Date:Created in 2018/7/3 16:57
 * @Author:
 * @Modified By: zjn 2018/7/3 16:57
 */
@Component()
@Aspect
public class DictItemServiceAspect {

    @Autowired
    MemCachedClient memCachedClient;
    @Autowired
    DictItemMapper dictItemMapper;

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.basic.DictItemService.insert(..))")
    private void insert() {}

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.basic.DictItemService.update(..))")
    private void update() {}

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.basic.DictItemService.delete(..))")
    private void delete() {}

    /**
    * 插入
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/3  17:09
    */
    @Around("insert()")
    public Object insert(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();

        DictItem dictItem = (DictItem) joinPoint.getArgs()[0];
        // "7:%d"   替换输出
        String key = CacheKey.key(CacheKey.K_ID_V_DICT_ITEM_NAME_LIST, dictItem.getDictCategoryId());
        memCachedClient.delete(key);
        return result;
    }
    /**
    * 更新
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/3  17:10
    */
    @Around("update()")
    public Object update(ProceedingJoinPoint joinPoint) throws Throwable {
        DictItem dictItem = (DictItem) joinPoint.getArgs()[0];
        Object result = joinPoint.proceed();

        String key = CacheKey.key(CacheKey.K_ID_V_DICT_ITEM_NAME_LIST, dictItem.getDictCategoryId());
        memCachedClient.delete(key);
        return result;
    }
    /**
    * 删除
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/3  17:10
    */
    @Around("delete()")
    public Object delete(ProceedingJoinPoint joinPoint) throws Throwable {
        Integer id = (Integer) joinPoint.getArgs()[0];
        DictItem dictItem = dictItemMapper.find(id);

        Object result = joinPoint.proceed();
        if(dictItem != null) {
            String key = CacheKey.key(CacheKey.K_ID_V_DICT_ITEM_NAME_LIST, dictItem.getDictCategoryId());
            memCachedClient.delete(key);
        }
        return result;
    }

}
